package com.jindianjichi.wx.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class WxChatMsgFactory implements InitializingBean {
    @Resource
   private List<WxChatMsgHandler> wxChatMsgHandlerList;

    private HashMap<WxChatMsgTypeEnum,WxChatMsgHandler> handlerHashMap=new HashMap<>();

    public WxChatMsgHandler getHandlerByMsgType(String msgType){
        WxChatMsgTypeEnum msgTypeEnum = WxChatMsgTypeEnum.getByMsgType(msgType);
        return handlerHashMap.get(msgTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WxChatMsgHandler wxChatMsgHandler : wxChatMsgHandlerList) {
           handlerHashMap.put(wxChatMsgHandler.getMsgType(),wxChatMsgHandler);
        }
    }
}
